package com.pay.controller.admin;

import com.github.binarywang.utils.qrcode.QrcodeUtils;
import com.google.common.io.Files;
import com.pay.model.auto.PayAccountExample;
import org.apache.commons.io.FileSystemUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageInfo;
import com.pay.common.base.BaseController;
import com.pay.common.domain.AjaxResult;
import com.pay.model.auto.PayAccount;
import com.pay.model.custom.TableSplitResult;
import com.pay.model.custom.Tablepar;
import com.pay.model.custom.TitleVo;
import com.pay.service.PayAccountService;
import io.swagger.annotations.Api;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.OutputStream;

@Controller
@RequestMapping("PayAccountController")
@Api(value = "支付账户")
public class PayAccountController extends BaseController {

    private String prefix = "admin/payAccount";
    @Autowired
    private PayAccountService payAccountService;

    @GetMapping("view")
    @RequiresPermissions("gen:payAccount:view")
    public String view(Model model) {
        String str = "支付账户";
        setTitle(model, new TitleVo("列表", str + "管理", true, "欢迎进入" + str + "页面", true, false));
        return prefix + "/list";
    }


    @PostMapping("list")
    @RequiresPermissions("gen:payAccount:list")
    @ResponseBody
    public Object list(Tablepar tablepar) {
        PageInfo<PayAccount> page = payAccountService.list(tablepar);
        TableSplitResult<PayAccount> result = new TableSplitResult<PayAccount>(page.getPageNum(), page.getTotal(), page.getList());
        return result;
    }

    /**
     * 新增
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        return prefix + "/add";
    }


    @PostMapping("add")
    @RequiresPermissions("gen:payAccount:add")
    @ResponseBody
    public AjaxResult add(PayAccount payAccount, @RequestParam("file") MultipartFile file) throws Exception {
        //创建临时文件
        File htmlFile = File.createTempFile("temp", ".html");
        file.transferTo(htmlFile);
        String qrcode = QrcodeUtils.decodeQrcode(htmlFile);
        payAccount.setPayUrl(qrcode);
        FileUtils.deleteQuietly(htmlFile);

        PayAccountExample payAccountExample = new PayAccountExample();
        payAccountExample.createCriteria().andPayUrlEqualTo(payAccount.getPayUrl());
        long count = payAccountService.countByExample(payAccountExample);
        if(count > 0){
            return error("当前收款码已经存在");
        }
        // 添加
        int b = payAccountService.insertSelective(payAccount);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }

    /**
     * 删除用户
     *
     * @param ids
     * @return
     */
    @PostMapping("remove")
    @RequiresPermissions("gen:payAccount:remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        int b = payAccountService.deleteByPrimaryKey(ids);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }

    /**
     * 检查用户
     *
     * @param payAccount
     * @return
     */
    @PostMapping("checkNameUnique")
    @ResponseBody
    public int checkNameUnique(PayAccount payAccount) {
        int b = payAccountService.checkNameUnique(payAccount);
        if (b > 0) {
            return 1;
        } else {
            return 0;
        }
    }


    /**
     * 修改跳转
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("PayAccount", payAccountService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }

    /**
     * 修改保存
     */
    @RequiresPermissions("gen:payAccount:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PayAccount record,
                       @RequestParam(value = "file", required = false) MultipartFile file) throws Exception {

        if(null != file){
            //创建临时文件
            File htmlFile = File.createTempFile("temp", ".html");
            file.transferTo(htmlFile);
            String qrcode = QrcodeUtils.decodeQrcode(htmlFile);
            FileUtils.deleteQuietly(htmlFile);

            if(!record.getPayUrl().equals(qrcode)){
                PayAccountExample payAccountExample = new PayAccountExample();
                payAccountExample.createCriteria().andPayUrlEqualTo(record.getPayUrl());
                long count = payAccountService.countByExample(payAccountExample);
                if(count > 0){
                    return error("当前收款码已经存在");
                }
                record.setPayUrl(qrcode);
            }
        }
        return toAjax(payAccountService.updateByPrimaryKeySelective(record));
    }


}
